<template>
  <div class="mod-config">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>品牌管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button  type="primary" @click="addOrUpdateHandle()">新增品牌</el-button>
        
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      style="width: 100%;">
         <el-table-column type="index" label="#"></el-table-column>
     <el-table-column
          property="logo"
          label="LOGO"
          width="115">
          <template slot-scope="scope">
      　　　　<img :src="scope.row.logo" class="headimag"/>
      　　</template>
        </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="品牌名" width="120">
      </el-table-column>
      <el-table-column
        prop="showStatus"
        header-align="center"
        align="center"
        label="显示状态" width="150">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.showStatus"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff0000"
            active-text="启用"
            inactive-text="禁用" disabled>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="firstLetter"
        header-align="center"
        align="center"
        label="检索字母" width="80">
      </el-table-column>
      <el-table-column
        prop="descript"
        header-align="center"
        align="center"
        label="品牌描述">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="290"
        label="操作">
        <template slot-scope="scope">
          <el-button type="warning" icon="el-icon-setting" size="mini" @click="relationHandle(scope.row)">关联分类</el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="addOrUpdateHandle(scope.row.brandId,scope.row)">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteHandle(scope.row.brandId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[5, 10,15, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" @updatedata="getDataList" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </el-card>
    <!-- 关联弹框 -->
    <el-dialog
      title="关联分类"
      :close-on-click-modal="false"
      :visible.sync="dialogTableVisible">
      <el-button type="primary" @click="addRelation()" >新增关联</el-button>
       <el-table :data="brandCategoryList" highlight-current-row  border>
        <el-table-column
        type="index"
         label="#"
          width="100">
        </el-table-column>
        <el-table-column
          property="brandName"
          label="品牌名"
          width="150">
        </el-table-column>
        <el-table-column
          property="categoryName"
          label="分类名">
        </el-table-column>
        <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteBrandHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="dialogTableVisible = false">取消</el-button> -->
        <el-button type="primary" @click="addRelationSubmit()">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 新增关联弹框 -->
    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      center>
       <el-cascader ref="cascader"
        v-model="brandRelation.categoryId"
        :options="goodsCategories" :props="props"
        @change="handleChange">
       </el-cascader>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="newAddRelation">确 定</el-button>
      </span>
    </el-dialog>
  </div>
  
  
</template>

<script>
  import AddOrUpdate from './components/AddOrUpdate'
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          showStatus:''
        },
        dataList: [
        ],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        // 当前弹框品牌ID
        selectBrandId:'0',
        // dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        // 关联弹框
        brandCategoryList:[{
          brandId:1
        }],
        dialogTableVisible:false,
        // 新增关联弹框
        centerDialogVisible:false,
        // 商品分类展示
        goodsCategories:[],
        // 级联选择器
        props:{
            value:"catId",
            label:'catName',
            children:"children"
          },
          // 新增关联中级联选择中的信息
          brandRelation:{
            categoryId:'',
            categoryName:'',
            brandId:'',
            brandName:''
          }
      }
    },
    components: {
      AddOrUpdate
    },
    created(){
      this.getDataList()
    },
    // activated () {
      
    // },
    methods: {
      // 获取数据列表
      async  getDataList () {
        // this.dataListLoading = true
           const {data:res} = await this.$http.get(`/xuanmall/brand/list?page=${this.pageIndex}&limit=${this.pageSize}&key=${this.dataForm.key}`)
          //  console.log(res)
           if(res.msg === 'success'){
             this.dataList = res.page.list
             this.totalPage = res.page.totalCount
           }else{
             this.dataList = []
             this.totalPage = 0
           }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 关联分类
      async  relationHandle(data){
        this.selectBrandId =data.brandId
        this.brandRelation.brandId = data.brandId
        this.brandRelation.brandName = data.name
        this.getbrandCategoryList()
        this.dialogTableVisible=true
      },
      // 获取品牌的已关联分类数据
       async  getbrandCategoryList(){
        //  console.log(this.selectBrandId)
        const {data:res} = await this.$http.get(`/xuanmall/categorybrandrelation/catelog/list?brandId=${this.selectBrandId}`)
        // console.log("ddd",res)
        this.brandCategoryList = res.data
      },
      // 新增关联按钮
      addRelation(){
        this.getCategoriesList()
        this.centerDialogVisible = true
      },
      // 获取级联分类数据
      async  getCategoriesList(){
      const {data:res} = await this.$http.get("/xuanmall/category/list/tree")
      this.goodsCategories = this.formatData(res.data)
    },
    // 格式化数据，递归将空的children置为undefined
      formatData(data) {
        for (var i = 0; i < data.length; i++) {
          if (data[i].children.length < 1) {
            data[i].children = undefined
          } else {
            this.formatData(data[i].children)
          }
        }
        return data
        },
    // 当选节点点击时触发
    handleChange(){
      var nodeInfo = this.$refs.cascader.getCheckedNodes()
      // console.log("noe",nodeInfo)
      this.brandRelation.categoryName = nodeInfo[0].data.catName;
      this.brandRelation.categoryId = nodeInfo[0].data.catId;
    },
      // 新增关联分类确定
      async  newAddRelation(){
        const {data:res} = await this.$http.post("/xuanmall/categorybrandrelation/save",this.brandRelation)
        
        if(res.msg === 'success'){
          this.getbrandCategoryList()
          this.centerDialogVisible = false
        }
        
      },
      // 新增关联信息提交
      addRelationSubmit(){
        this.dialogTableVisible =false
      },
      // 新增 / 修改
      addOrUpdateHandle (id,data) {  
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          console.log("data",data)
          this.$refs.addOrUpdate.init(id,data)
        })
        this.getDataList()
      },
      deleteBrandHandle(id){
        var ids =  [id] 
        this.$confirm(`确定删除该关联吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {data:res} = await this.$http.delete("/xuanmall/categorybrandrelation/delete",{data: ids})
          // console.log("guanliansha",res)
          if(res.msg === 'success'){
            this.getbrandCategoryList()
            this.$message.success("删除成功")
          }
        }).catch(() => {
        });
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.brandId
        })
        this.$confirm(`确定对id=${ids.join(',')}的品牌进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.delete("/xuanmall/brand/delete",{data: ids})
          .then(Response=>{
            if(Response.data.msg === 'success'){
              this.getDataList()
              this.$message.success("删除成功")
            }
          })
        }).catch(() => {
        });
      }
    }
  }
</script>
<style lang="less" scoped>

.headimag{
  height: 50px;
  width: 100px;
}
</style>
